<template>
  <Layout>
    <div class="ai-chat-container">
            <!-- 科技感背景元素 -->
      <div class="tech-background">
        <div class="tech-circle"></div>
        <div class="tech-circle tech-circle-2"></div>
        <div class="tech-lines"></div>
        <div class="tech-particles">
          <span></span><span></span><span></span><span></span><span></span>
          <span></span><span></span><span></span><span></span><span></span>
        </div>
      </div>
      <div class="model-selector">
        <div class="model-title">闪职金融 智能助手</div>
        <el-select v-model="selectedModel" placeholder="选择服务类型" class="model-select">
          <el-option
            v-for="model in availableModels"
            :key="model.value"
            :label="model.label"
            :value="model.value"
          />
        </el-select>
      </div>

      <div class="chat-window" ref="chatWindow">
        <!-- 科技感装饰元素 -->
        <div class="tech-decoration">
          <div class="tech-dot"></div>
          <div class="tech-dot"></div>
          <div class="tech-dot"></div>
          <div class="tech-line"></div>
        </div>
        <div v-for="(message, index) in chatHistory" :key="index" class="message-container">
          <div :class="['message', message.role === 'user' ? 'user-message' : 'ai-message']">
            <div class="message-avatar">
              <el-avatar :size="40" :src="message.role === 'user' ? userAvatar : aiAvatar" />
            </div>
            <div class="message-content">
              <div class="message-header">
                <span class="message-sender">{{ message.role === 'user' ? '您' : '闪职AI助手' }}</span>
                <span class="message-time">{{ formatTime(message.timestamp) }}</span>
              </div>
              <div class="message-text" v-html="formatMessage(message.content)" :class="{'message-animation': message.role === 'assistant'}"></div>
            </div>
          </div>
        </div>
        <div v-if="isTyping" class="message-container">
          <div class="message ai-message">
            <div class="message-avatar">
              <el-avatar :size="40" :src="aiAvatar" />
            </div>
            <div class="message-content">
              <div class="message-header">
                <span class="message-sender">闪职AI助手</span>
              </div>
              <div class="typing-indicator">
                <span></span>
                <span></span>
                <span></span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="input-area">
        <el-input
          v-model="userInput"
          type="textarea"
          :rows="3"
          placeholder="请输入您的问题..."
          resize="none"
          @keyup.enter.ctrl="sendMessage"
          class="ai-input"
        />
        <div class="input-actions">
          <span class="input-tip">按 Ctrl+Enter 发送</span>
          <el-button type="primary" :disabled="!userInput.trim()" @click="sendMessage" class="send-button">
            <el-icon class="send-icon"><Position /></el-icon> 发送
          </el-button>
        </div>
      </div>
    </div>
  </Layout>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue';
import { marked } from 'marked';
import DOMPurify from 'dompurify';
import Layout from '@/components/layout/Layout.vue';
import { Position } from '@element-plus/icons-vue';
const availableModels = [
  { value: 'sz-finance', label: '闪职DeepSeek金融助手' },
  { value: 'sz-banking', label: '闪职银行业务' },
  { value: 'sz-investment', label: '闪职投资顾问' },
];

const selectedModel = ref('sz-finance');
const userInput = ref('');
const chatHistory = ref([]);
const isTyping = ref(false);
const chatWindow = ref(null);

// 头像URL
const userAvatar = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png';
const aiAvatar = 'https://cdn.pixabay.com/photo/2023/05/23/15/26/ai-8013128_1280.jpg';

// 模拟发送消息
const sendMessage = async () => {
  if (!userInput.value.trim()) return;
  
  // 添加用户消息
  chatHistory.value.push({
    role: 'user',
    content: userInput.value,
    timestamp: new Date()
  });
  
  // 清空输入框
  const userQuestion = userInput.value;
  userInput.value = '';
  
  // 滚动到底部
  await nextTick();
  scrollToBottom();
  
  // 模拟AI思考
  isTyping.value = true;
  
  // 模拟API调用延迟
  setTimeout(() => {
    isTyping.value = false;
    
    // 模拟AI回复
    chatHistory.value.push({
      role: 'assistant',
      content: generateAIResponse(userQuestion, selectedModel.value),
      timestamp: new Date()
    });
    
    // 滚动到底部
    nextTick(() => {
      scrollToBottom();
    });
  }, 1500);
};

// 模拟AI回复生成
const generateAIResponse = (question, model) => {
  // 模拟不同模型的回复风格
  const responses = {
    'sz-finance': [
      `您好！我是闪职AI助手，很高兴为您服务。关于"${question}"，我可以提供以下金融信息：\n\n根据我的知识库，这是一个很好的问题。在金融领域，我们通常建议客户考虑多元化投资策略，包括股票、债券和其他金融工具的组合。这样可以降低风险，提高长期收益的稳定性。`,
      `感谢您的提问！"${question}"是一个很有深度的金融话题。\n\n从金融角度来看，市场波动是常态，但长期投资策略通常能够帮助投资者度过短期波动。我们建议根据个人风险承受能力和投资目标制定合适的投资计划。`,
      `关于"${question}"，我想分享一些金融见解：\n\n1. 首先，资产配置是投资成功的关键\n2. 其次，定期检视和调整投资组合很重要\n3. 最后，长期投资通常优于短期投机\n\n希望我的回答对您有所帮助！`
    ],
    'sz-banking': [
      `关于"${question}"的银行业务问题，我可以为您提供以下信息：\n\n闪职虚拟银行提供全方位的银行服务，包括账户管理、转账汇款、贷款申请等。您可以通过我们的线上平台轻松完成各种银行操作，安全便捷。`,
      `您询问的"${question}"涉及银行业务，我们的服务包括：\n\n1. 账户管理：查询余额、交易记录\n2. 转账服务：行内转账、跨行转账\n3. 理财产品：定期存款、基金投资\n4. 贷款服务：个人贷款、住房贷款\n\n需要了解更多详情或办理业务，请告诉我您的具体需求。`
    ],
    'sz-investment': [
      `关于"${question}"的投资问题，作为闪职投资顾问，我建议：\n\n投资应当基于个人风险承受能力和财务目标。多元化配置资产可以有效分散风险，提高投资组合的稳定性。我们可以根据您的具体情况，为您提供个性化的投资建议。`,
      `"${question}"是个很好的投资话题。作为投资顾问，我认为：\n\n1. 了解自己的风险承受能力是投资的第一步\n2. 分散投资于不同资产类别可以降低整体风险\n3. 长期投资通常能获得更稳定的回报\n\n如果您需要更具体的投资建议，可以详细告诉我您的财务状况和投资目标。`
    ]
  };
  
  // 随机选择一个回复
  const modelResponses = responses[model] || responses['deepseek-chat'];
  const randomIndex = Math.floor(Math.random() * modelResponses.length);
  return modelResponses[randomIndex];
};

// 格式化消息内容（支持Markdown）
const formatMessage = (content) => {
  // 使用marked解析Markdown，并用DOMPurify清理HTML
  return DOMPurify.sanitize(marked.parse(content));
};

// 格式化时间
const formatTime = (timestamp) => {
  return new Intl.DateTimeFormat('zh-CN', {
    hour: '2-digit',
    minute: '2-digit'
  }).format(timestamp);
};

// 滚动到底部
const scrollToBottom = () => {
  if (chatWindow.value) {
    chatWindow.value.scrollTop = chatWindow.value.scrollHeight;
  }
};

// 初始化聊天历史
onMounted(() => {
  // 添加欢迎消息
  chatHistory.value = [
    {
      role: 'assistant',
      content: '欢迎打开闪职AI智能助手，有金融知识可以问我 我将为您解答；如果有银行卡绑定和转账操作也可以直接告诉我 我来帮您实现~',
      timestamp: new Date()
    }
  ];
});
</script>

<style scoped>
/* 全局样式变量 - 浅色主题 */
:root {
  --primary-color: #3182ce;
  --primary-gradient: linear-gradient(135deg, #4299e1, #2b6cb0);
  --light-bg: #f7fafc;
  --card-bg: #ffffff;
  --text-color: #2d3748;
  --accent-color: #3182ce;
  --glow-color: rgba(49, 130, 206, 0.5);
  --border-color: rgba(49, 130, 206, 0.3);
  --shadow-color: rgba(0, 0, 0, 0.1);
}
.ai-chat-container {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 120px);
  background-color: var(--light-bg);
  border-radius: 12px;
  box-shadow: 0 8px 24px var(--shadow-color), 0 0 30px var(--glow-color);
  overflow: hidden;
  margin-bottom: 20px;
  border: 1px solid var(--border-color);
  position: relative;
}

/* 科技感背景元素 - 增强版 */
.tech-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 0;
}

.tech-circle {
  position: absolute;
  top: -150px;
  right: -150px;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  border: 2px solid var(--border-color);
  box-shadow: 0 0 50px var(--glow-color);
  opacity: 0.6;
  animation: rotate 20s linear infinite;
}

.tech-circle-2 {
  top: auto;
  right: auto;
  bottom: -100px;
  left: -100px;
  width: 200px;
  height: 200px;
  animation-duration: 15s;
  animation-direction: reverse;
  opacity: 0.4;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.tech-lines {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  background-image: linear-gradient(0deg, rgba(49, 130, 206, 0.08) 1px, transparent 1px),
                    linear-gradient(90deg, rgba(49, 130, 206, 0.08) 1px, transparent 1px);
  background-size: 30px 30px;
  background-position: center center;
  opacity: 0.5;
  animation: pulse-grid 8s ease-in-out infinite;
}

.tech-particles span {
  position: absolute;
  display: block;
  width: 4px;
  height: 4px;
  background: var(--primary-color);
  border-radius: 50%;
  box-shadow: 0 0 10px var(--glow-color), 0 0 20px var(--glow-color);
  animation: float 15s linear infinite;
  opacity: 0.6;
}

.tech-particles span:nth-child(1) { top: 10%; left: 20%; animation-delay: 0s; }
.tech-particles span:nth-child(2) { top: 20%; left: 80%; animation-delay: 2s; }
.tech-particles span:nth-child(3) { top: 40%; left: 40%; animation-delay: 4s; }
.tech-particles span:nth-child(4) { top: 60%; left: 10%; animation-delay: 6s; }
.tech-particles span:nth-child(5) { top: 80%; left: 50%; animation-delay: 8s; }
.tech-particles span:nth-child(6) { top: 30%; left: 30%; animation-delay: 10s; }
.tech-particles span:nth-child(7) { top: 70%; left: 20%; animation-delay: 12s; }
.tech-particles span:nth-child(8) { top: 50%; left: 70%; animation-delay: 14s; }
.tech-particles span:nth-child(9) { top: 15%; left: 60%; animation-delay: 16s; }
.tech-particles span:nth-child(10) { top: 90%; left: 90%; animation-delay: 18s; }

@keyframes float {
  0% {
    transform: translateY(0) translateX(0) scale(1);
    opacity: 0.6;
  }
  50% {
    transform: translateY(-20px) translateX(10px) scale(1.2);
    opacity: 0.8;
  }
  100% {
    transform: translateY(0) translateX(0) scale(1);
    opacity: 0.6;
  }
}

@keyframes pulse-grid {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 0.5; }
}

.model-selector {
  padding: 16px;
  border-bottom: 1px solid var(--border-color);
  background: linear-gradient(to right, #e6f2ff, #f0f9ff);
  box-shadow: 0 4px 12px var(--shadow-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  z-index: 1;
}

.model-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--primary-color);
  text-shadow: 0 0 10px var(--glow-color);
  letter-spacing: 1px;
  display: flex;
  align-items: center;
}

.model-title::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  background-color: var(--primary-color);
  border-radius: 50%;
  margin-right: 8px;
  box-shadow: 0 0 10px var(--glow-color);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(49, 130, 206, 0.7);
  }
  70% {
    box-shadow: 0 0 0 10px rgba(49, 130, 206, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(49, 130, 206, 0);
  }
}

.model-select {
  width: 220px;
}

.model-select :deep(.el-input__wrapper) {
  background-color: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--border-color);
  box-shadow: 0 0 10px var(--glow-color);
}

.model-select :deep(.el-input__inner) {
  color: var(--text-color);
}

.model-select :deep(.el-select__popper) {
  background-color: var(--card-bg);
  border: 1px solid var(--border-color);
}

.model-select :deep(.el-select-dropdown__item) {
  color: var(--text-color);
}

.model-select :deep(.el-select-dropdown__item.selected) {
  color: var(--primary-color);
  background-color: rgba(49, 130, 206, 0.1);
}

.chat-window {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background-color: var(--light-bg);
  background-image: radial-gradient(circle at 25px 25px, rgba(49, 130, 206, 0.08) 2%, transparent 0%), 
                    radial-gradient(circle at 75px 75px, rgba(49, 130, 206, 0.08) 2%, transparent 0%);
  background-size: 100px 100px;
  position: relative;
  scroll-behavior: smooth;
}

/* 科技感装饰元素 - 增强版 */
.tech-decoration {
  position: absolute;
  top: 20px;
  right: 20px;
  display: flex;
  gap: 8px;
  z-index: 0;
  align-items: center;
}

.tech-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: var(--primary-color);
  opacity: 0.7;
  animation: blink 3s infinite alternate;
}

.tech-dot:nth-child(2) {
  animation-delay: 0.5s;
}

.tech-dot:nth-child(3) {
  animation-delay: 1s;
}

.tech-line {
  width: 40px;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--primary-color), transparent);
  opacity: 0.7;
  animation: pulse-line 4s infinite;
  margin-left: 5px;
}

@keyframes pulse-line {
  0%, 100% { width: 40px; opacity: 0.5; }
  50% { width: 60px; opacity: 0.8; }
}

@keyframes blink {
  0%, 80% {
    opacity: 0.3;
    transform: scale(1);
  }
  100% {
    opacity: 0.8;
    transform: scale(1.5);
    box-shadow: 0 0 8px var(--glow-color);
  }
}

.message-container {
  margin-bottom: 20px;
}

.message {
  display: flex;
  max-width: 85%;
}

.user-message {
  margin-left: auto;
  flex-direction: row-reverse;
}

.message-avatar {
  margin: 0 12px;
}

.message-content {
  background-color: var(--card-bg);
  padding: 12px 16px;
  border-radius: 12px;
  box-shadow: 0 4px 12px var(--shadow-color);
  min-width: 200px;
  border: 1px solid var(--border-color);
  color: var(--text-color);
  position: relative;
  overflow: hidden;
}

.message-content::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--primary-color), transparent);
  opacity: 0.3;
}

.message-animation {
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.user-message .message-content {
  background: linear-gradient(135deg, #ebf8ff, #bee3f8);
  border: 1px solid var(--border-color);
  color: #2c5282;
}

.ai-message .message-content {
  background: linear-gradient(135deg, #ffffff, #f7fafc);
  border: 1px solid var(--border-color);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05), 0 0 2px var(--glow-color);
}

.message-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 14px;
}

.message-sender {
  font-weight: 600;
  color: var(--primary-color);
  text-shadow: 0 0 5px var(--glow-color);
  position: relative;
  display: inline-flex;
  align-items: center;
}

.message-sender::after {
  content: '';
  display: inline-block;
  width: 4px;
  height: 4px;
  background-color: var(--primary-color);
  border-radius: 50%;
  margin-left: 6px;
  box-shadow: 0 0 5px var(--glow-color);
  animation: pulse 2s infinite;
}

.message-time {
  color: #718096;
  font-size: 12px;
}

.message-text {
  line-height: 1.5;
  word-break: break-word;
}

/* 支持Markdown渲染的样式 */
.message-text :deep(pre) {
  background-color: #f1f5f9;
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  border: 1px solid var(--border-color);
  box-shadow: 0 4px 12px var(--shadow-color);
}

.message-text :deep(code) {
  font-family: 'JetBrains Mono', monospace;
  background-color: #edf2f7;
  padding: 2px 4px;
  border-radius: 4px;
  color: var(--primary-color);
  text-shadow: none;
}

.message-text :deep(p) {
  margin: 8px 0;
}

.message-text :deep(ul), .message-text :deep(ol) {
  padding-left: 20px;
}

.input-area {
  padding: 16px;
  border-top: 1px solid var(--border-color);
  background: linear-gradient(to right, #e6f2ff, #f0f9ff);
  box-shadow: 0 -4px 12px var(--shadow-color);
}

.input-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
}

.input-tip {
  color: var(--primary-color);
  font-size: 12px;
}

/* 打字指示器动画 */
.typing-indicator {
  display: flex;
  padding: 6px 0;
}

.typing-indicator span {
  height: 8px;
  width: 8px;
  background-color: var(--primary-color);
  border-radius: 50%;
  display: inline-block;
  margin: 0 2px;
  animation: typing 1.4s infinite ease-in-out;
  box-shadow: 0 0 10px var(--glow-color);
}

.typing-indicator span:nth-child(1) {
  animation-delay: 0s;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing {
  0% {
    transform: translateY(0);
    opacity: 0.4;
    box-shadow: 0 0 5px var(--glow-color);
  }
  50% {
    transform: translateY(-5px);
    opacity: 0.8;
    box-shadow: 0 0 15px var(--glow-color);
  }
  100% {
    transform: translateY(0);
    opacity: 0.4;
    box-shadow: 0 0 5px var(--glow-color);
  }
}

/* 响应式设计 */
/* 输入框样式 */
.ai-input :deep(.el-textarea__inner) {
  background-color: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--border-color);
  color: var(--text-color);
  border-radius: 8px;
  transition: all 0.3s ease;
  resize: none;
}

.ai-input :deep(.el-textarea__inner:focus) {
  border-color: var(--primary-color);
  box-shadow: 0 0 15px var(--glow-color);
}

/* 发送按钮样式 */
.send-button {
  background: var(--primary-gradient);
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px var(--shadow-color);
}

.send-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px var(--shadow-color);
  background: linear-gradient(135deg, #2b6cb0, #4299e1);
}

.send-button:active {
  transform: translateY(0);
}

.send-icon {
  margin-right: 5px;
}

@media (max-width: 768px) {
  .message {
    max-width: 95%;
  }
  
  .message-content {
    min-width: 150px;
  }
}
</style>